using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using System.Configuration;

public class Client{

	private DataAccess access;
	
	public Client(){
		
	}
	
	public bool AddStudent(string FirstName,string MiddleName,string LastName,string Gender, string Address,string Residency, string ContactNo,string College,string ProgCode,string MajorCode,string ScholarshipType){
      
        SqlConnection connection =
        new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
        connection.Open();
		SqlCommand cmd = new SqlCommand("AddStudent", connection);
		cmd.CommandType = CommandType.StoredProcedure;
		cmd.Parameters.Add("@fName", SqlDbType.Char).Value = FirstName;
		cmd.Parameters.Add("@mName", SqlDbType.Char).Value = MiddleName;
		cmd.Parameters.Add("@lName", SqlDbType.Char).Value = LastName;
		cmd.Parameters.Add("@gender", SqlDbType.Char).Value = Gender;
		cmd.Parameters.Add("@address", SqlDbType.Char).Value = Address;
		cmd.Parameters.Add("@residency", SqlDbType.Char).Value = Residency;
		cmd.Parameters.Add("@contactNo", SqlDbType.Char).Value = ContactNo;
		cmd.Parameters.Add("@college", SqlDbType.Char).Value = College;
		cmd.Parameters.Add("@progCode", SqlDbType.Char).Value = ProgCode;
		cmd.Parameters.Add("@majorCode", SqlDbType.Char).Value = MajorCode;
		cmd.Parameters.Add("@scholarshipType", SqlDbType.Char).Value = ScholarshipType;
		int status = cmd.ExecuteNonQuery();
        connection.Close();
        if (status > 0) { return true; } else return false;
	}

    public bool UpdateStudent(Student student)
    {
        SqlConnection connection = 
            new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
        connection.Open();
        
        SqlCommand cmd = new SqlCommand("UpdateStudent", connection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@studentId", SqlDbType.Int).Value = student.getStudentId();
        cmd.Parameters.Add("@fName", SqlDbType.VarChar).Value = student.getFirstName();
        cmd.Parameters.Add("@mName", SqlDbType.VarChar).Value = student.getMiddleName();
        cmd.Parameters.Add("@lName", SqlDbType.VarChar).Value = student.getLastName();
        cmd.Parameters.Add("@gender", SqlDbType.VarChar).Value = student.getGender();
        cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = student.getAddress();
        cmd.Parameters.Add("@residency", SqlDbType.VarChar).Value = student.getResidency();
        cmd.Parameters.Add("@contactNo", SqlDbType.VarChar).Value = student.getContactNo();
        cmd.Parameters.Add("@college", SqlDbType.VarChar).Value = student.getCollege();
        cmd.Parameters.Add("@progCode", SqlDbType.VarChar).Value = student.getProgramCode();
        cmd.Parameters.Add("@majorCode", SqlDbType.VarChar).Value = student.getMajorCode();
        cmd.Parameters.Add("@scholarshipType", SqlDbType.VarChar).Value = student.getScholarship();
        int result = cmd.ExecuteNonQuery();
        connection.Close();
        if (result > 0)
            return true;
        else
            return false;
    }

    public Student ViewStudent(int StudentId)
    {
        Student student;
        SqlConnection connection =
        new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
        connection.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Student WHERE StudentId = @studentId", connection);
        
        cmd.Parameters.Add("@studentId", SqlDbType.Int).Value = StudentId;
 
        SqlDataReader drStud = cmd.ExecuteReader();
        drStud.Read();
            student = new Student(1,
            drStud["FName"].ToString(),
            drStud["LName"].ToString(),
            drStud["MName"].ToString(),
            drStud["Address"].ToString(),
            drStud["Residency"].ToString(),
            drStud["College"].ToString(),
            drStud["Prog_Code"].ToString(),
            drStud["Major_COde"].ToString(),
            drStud["ScholarshipType"].ToString(),
            drStud["Gender"].ToString(),
            System.Convert.ToInt32(drStud["ContactNo"]));

        drStud.Close();
        connection.Close();
        return student;
    }
}
